﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class site_mag_APIList : System.Web.UI.Page
{
    PagedDataSource pds1 = new PagedDataSource();
    public int pageCount;//总页数
    public int currentPage = 1;//当前页

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            /*
            string userName = Convert.ToString(Session["userName"]);
            if (userName == "")
            {
                Response.Redirect("index.aspx");
            }
            */

            bind();

        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddAPI.aspx");
    }


    private void bind()//绑定数据源
    {
        Maticsoft.BLL.bll_api ns = new Maticsoft.BLL.bll_api();
        DataSet ds = ns.GetList("");
        //创建数据源
        pds1.DataSource = ds.Tables[0].DefaultView;
        //允许分页
        pds1.AllowPaging = true;
        //设置每页显示记录数
        pds1.PageSize = int.Parse("10");
        //获取总页数
        pageCount = pds1.PageCount;
        this.lblall.Text = pageCount.ToString();
        pds1.CurrentPageIndex = currentPage - 1;
        //当前页
        this.lblcurrent.Text = Convert.ToString(currentPage);
        //数据绑定
        this.Repeat1.DataSource = pds1;
        this.Repeat1.DataBind();
        Pagecolor();

    }

    //首页
    protected void lblfirst_Click(object sender, EventArgs e)
    {
        if (this.lblcurrent.Text == "1")
        { }
        else
        {
            currentPage = 1;
            bind();
        }
    }

    //上一页
    protected void lbtnform_Click(object sender, EventArgs e)
    {
        if (this.lblcurrent.Text != "1")
        {
            currentPage = int.Parse(this.lblcurrent.Text) - 1;
            this.lblcurrent.Text = currentPage.ToString();
            bind();
        }
    }
    //下一页
    protected void lbtnnext_Click(object sender, EventArgs e)
    {
        if (this.lblall.Text == this.lblcurrent.Text)
        {
        }
        else
        {
            currentPage = int.Parse(this.lblcurrent.Text) + 1;
            this.lblcurrent.Text = currentPage.ToString();
            bind();
        }
    }

    //尾页
    protected void lbllast_Click(object sender, EventArgs e)
    {
        if (this.lblall.Text != this.lblcurrent.Text)
        {
            this.lblcurrent.Text = this.lblall.Text;
            currentPage = int.Parse(this.lblall.Text);
            bind();
        }
    }

    private void Pagecolor()
    {
        if (pds1.CurrentPageIndex == 0)
        {
            lblfirst.Enabled = false;
            lbtnform.Enabled = false;
            lblfirst.ForeColor = System.Drawing.ColorTranslator.FromHtml("#aaaaaa");
            lbtnform.ForeColor = System.Drawing.ColorTranslator.FromHtml("#aaaaaa");

            if (pageCount <= 1)
            {
                lbllast.Enabled = false;
                lbtnnext.Enabled = false;
                lbllast.ForeColor = System.Drawing.ColorTranslator.FromHtml("#aaaaaa");
                lbtnnext.ForeColor = System.Drawing.ColorTranslator.FromHtml("#aaaaaa");
            }
            else
            {
                lbllast.Enabled = true;
                lbtnnext.Enabled = true;
                lbllast.ForeColor = System.Drawing.ColorTranslator.FromHtml("#6E050C");
                lbtnnext.ForeColor = System.Drawing.ColorTranslator.FromHtml("#6E050C");
            }
        }
        else if (pds1.CurrentPageIndex == pageCount - 1)
        {
            lblfirst.Enabled = true;
            lbtnform.Enabled = true;
            lblfirst.ForeColor = System.Drawing.ColorTranslator.FromHtml("#6E050C");
            lbtnform.ForeColor = System.Drawing.ColorTranslator.FromHtml("#6E050C");
            lbllast.Enabled = false;
            lbtnnext.Enabled = false;
            lbllast.ForeColor = System.Drawing.ColorTranslator.FromHtml("#aaaaaa");
            lbtnnext.ForeColor = System.Drawing.ColorTranslator.FromHtml("#aaaaaa");

        }
        else if (pds1.CurrentPageIndex != pageCount - 1 || pds1.CurrentPageIndex != 0)
        {
            lblfirst.Enabled = true;
            lbtnform.Enabled = true;
            lblfirst.ForeColor = System.Drawing.ColorTranslator.FromHtml("#6E050C");
            lbtnform.ForeColor = System.Drawing.ColorTranslator.FromHtml("#6E050C");
            lbllast.Enabled = true;
            lbtnnext.Enabled = true;
            lbllast.ForeColor = System.Drawing.ColorTranslator.FromHtml("#6E050C");
            lbtnnext.ForeColor = System.Drawing.ColorTranslator.FromHtml("#6E050C");
        }
    }


    protected void btall_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem RPItem in this.Repeat1.Items)
        {
            CheckBox ChB = (CheckBox)RPItem.FindControl("cbx");
            if (!ChB.Checked)
            {
                ChB.Checked = true;

            }
        }
    }

    protected void btno_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem RPItem in this.Repeat1.Items)
        {
            CheckBox ChB = (CheckBox)RPItem.FindControl("cbx");
            if (!ChB.Checked)
            {
                ChB.Checked = true;

            }
            else
            {
                ChB.Checked = false;
            }
        }
    }

    protected void btnoselect_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem RPItem in this.Repeat1.Items)
        {
            CheckBox ChB = (CheckBox)RPItem.FindControl("cbx");
            if (ChB.Checked)
            {
                ChB.Checked = false;

            }

        }
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        string delId = "";
        //先遍历取得选中项
        foreach (RepeaterItem RPItem in this.Repeat1.Items)
        {
            CheckBox cbx = (CheckBox)RPItem.FindControl("cbx");
            Label lbl = (Label)RPItem.FindControl("lbl");
            if (cbx != null)
            {
                if (cbx.Checked)
                {
                    delId += lbl.Text + ",";
                }
            }
        }
        //去掉最后一个,
        delId = (delId + ")").Replace(",)", "");
        string sql = " delete * from api where id in(" + delId + ")";
        try
        {
            if (Maticsoft.DBUtility.DbHelperOleDb.ExecuteSql(sql) > 0)
            { }
            bind();
        }
        catch (Exception ex)
        {

            MessageBox.Show(this, "无API可删除！");
            return;
        }

    }


}